from django.http import JsonResponse
from django.shortcuts import render, redirect

from modular.mysqldb.Password import AES_ENCRYPT
from modular.mysqldb.logindb import Users


def login(request):
    if request.method == 'GET':
        # table = Users.objects.all()
        return render(request, 'login.html')
    else:
        aes_encrypt = AES_ENCRYPT()
        user = request.POST.get('account')
        pwd = request.POST.get('password')
        pwd = str(aes_encrypt.jiami(pwd))
        user = Users.objects.filter(account=user,password=pwd)
        if user.count() != 0:
            data = {
                'code': '200',
                'context': '登录成功',
                'other': str(user.values("name")[0].get('name'))
            }
            return JsonResponse(data)
        else:
            data = {
                'code': '500',
                'context': '登录失败',
                'other': ''
            }
            return JsonResponse(data)
